const Blogtype = require("./models/Blogtype");

exports.addBlogtype = async (info) => {
    const ins = await Blogtype.create(info);
    return ins.toJSON();
}

exports.getBlogtypes = async () => {
    const res = await Blogtype.findAll();
    return JSON.parse(JSON.stringify(res));
}

exports.getBlogtypeById = async (id) => {
    const ins = await Blogtype.findByPk(id);
    if(ins){
        return ins.toJSON();
    }
    return null;
}

exports.updateBlogtype = async (id, newInfo) => {
    await Blogtype.update(newInfo, {
        where: {
            id
        }
    })
    const ins = await Blogtype.findByPk(id);
    return ins.toJSON();
}

exports.deleteBlogtype = async (id) => {
    return await Blogtype.destroy({
        where: {
            id
        }
    });
}

exports.addBlogtypeCount = async(id) => {
    const ins = await Blogtype.findByPk(id);
    ins.articleCount++;
    await ins.save();
    return ins;
}
